package com.bojin.studio.ubattery;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

public class tabBatteryEstimate extends Activity {
    /** Called when the activity is first created. */	
	   ExpandableListView elv;
	   ArrayList<HashMap<String, Object>> listItem;
	   ArrayList<ArrayList<HashMap<String, Object>>> childlistItem;
	   SimpleExpandableListAdapter listItemAdapter;
	   SharedPreferences sharedPreferences;
	   
	   private BroadcastReceiver mBatteryInfoReceiver=new BroadcastReceiver()
	   {
		  @Override
		  public void onReceive(Context context, Intent intent) {
		   // TODO Auto-generated method stub		 
			  if(!(sharedPreferences.getBoolean("hasBSize",false)))
		       {
		        String action=intent.getAction();

		        if(Intent.ACTION_BATTERY_CHANGED.equals(action))
		        {
		         int mLevel=intent.getIntExtra("level",0);
		         int mScale=intent.getIntExtra("scale",100);	
                 updateList(mLevel*100/mScale);
                }
		       } 
		     }
		 };
	   
	   @Override
	   public void onCreate(Bundle savedInstanceState) {
	       super.onCreate(savedInstanceState);
	       setContentView(R.layout.estimate);
	       elv=(ExpandableListView)findViewById(R.id.expandableListView1);
	       registerReceiver(mBatteryInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
	       sharedPreferences = PreferenceManager.getDefaultSharedPreferences(tabBatteryEstimate.this);
	      
	       if(sharedPreferences.getBoolean("hasBSize",false))
	       {
	    	   LayoutInflater factory=LayoutInflater.from(tabBatteryEstimate.this);
	           final View DialView=factory.inflate(R.layout.diag,null);
	           Dialog dialog=new AlertDialog.Builder(tabBatteryEstimate.this)
	           .setTitle(getResources().getString(R.string.inp))
	           .setMessage(getResources().getString(R.string.inp1))
	           .setView(DialView)
	           .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
	   			
	   			@Override
	   			public void onClick(DialogInterface dialog, int which) {
	   				// TODO Auto-generated method stub
	   				EditText Aname=(EditText)DialView.findViewById(R.id.editText1);
	   				try{
	   				Integer.parseInt(Aname.getText().toString());
	   				SharedPreferences.Editor editor=sharedPreferences.edit();
	   				editor.putBoolean("hasBSize", false);
	   				editor.putString("Name", Aname.getText().toString()); 
	   				editor.commit();     				
	   				updateList(100);
	   				}
	   				catch(Exception e)
	   				{
	   					SharedPreferences.Editor editor=sharedPreferences.edit();
		   				editor.putBoolean("hasBSize", true);
		   				//editor.putString("Name", Aname.getText().toString()); 
		   				editor.commit();  	
	   					Toast.makeText(tabBatteryEstimate.this, "Error Format", Toast.LENGTH_SHORT).show();
	   					finish();
	   				}
	   				
	   					
	   			}
	   		})
	            .setNegativeButton(getResources().getString(R.string.cance), new DialogInterface.OnClickListener() {
	   			
	   			@Override
	   			public void onClick(DialogInterface dialog, int which) {				
	   				// TODO Auto-generated method stub
	   				finish();
	   			}
	   		}).create();
	           dialog.show();
	           
	       }
	   }
	   
	   @Override
	    public void onDestroy() {
	    	unregisterReceiver(mBatteryInfoReceiver);
	    	super.onDestroy();  
	    }
	   
	   HashMap<String, Object> addInfo(String title,String content)
	    {
	    	HashMap<String, Object> map = new HashMap<String, Object>();
	        map.put("title", title);   
			map.put("content", content);  
	    	return map;
	    }
	   
	   private ArrayList<HashMap<String, Object>> estimate_data(String data)
	    {
	    	ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
	    	listItem.add(addInfo(getResources().getString(R.string.eb)+" : ",data+"mAh"));
	    	
	    	return listItem;
	    }
	   
	    private ArrayList<HashMap<String, Object>> bsize()
	    {
	    	ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
	    	listItem.add(addInfo("mAh : ",getResources().getString(R.string.bsize1)));
	    	
	    	return listItem;
	    }
	    
	    void updateList(int cLevel)
	    {
                   String batterySize=sharedPreferences.getString("Name","1200");
      		       int bs=Integer.parseInt(batterySize);
      		       int c3g=(int)((bs/300.0)*(cLevel/100.0)*60)/1;
      		       int c2g=(int)((bs/250.0)*(cLevel/100.0)*60)/1;
      		       int pVideo=(int)((bs/400.0)*(cLevel/100.0)*60)/1;
      		       int pMusic=(int)((bs/150.0)*(cLevel/100.0)*60)/1;
      		       int pNet=(int)((bs/150.0)*(cLevel/100.0)*60)/1;
      		       int pStandby=(int)((bs/5.0)*(cLevel/100.0)*60)/1;
      		       listItem = new ArrayList<HashMap<String, Object>>();
      		       listItem.add(addInfo(getResources().getString(R.string.b_size)+" :",batterySize+"mAh"));
      		       listItem.add(addInfo(getResources().getString(R.string.c_3g)+" :",c3g/60+getResources().getString(R.string.shr)+c3g%60+getResources().getString(R.string.smin)));
      		       listItem.add(addInfo(getResources().getString(R.string.c_2g)+" :",c2g/60+getResources().getString(R.string.shr)+c2g%60+getResources().getString(R.string.smin)));
      		       listItem.add(addInfo(getResources().getString(R.string.p_video)+" :",pVideo/60+getResources().getString(R.string.shr)+pVideo%60+getResources().getString(R.string.smin)));
      		       listItem.add(addInfo(getResources().getString(R.string.p_music)+" :",pMusic/60+getResources().getString(R.string.shr)+pMusic%60+getResources().getString(R.string.smin)));
      		       listItem.add(addInfo(getResources().getString(R.string.p_internet)+" :",pNet/60+getResources().getString(R.string.shr)+pNet%60+getResources().getString(R.string.smin)));
      		       listItem.add(addInfo(getResources().getString(R.string.p_standby)+" :",pStandby/60+getResources().getString(R.string.shr)+pStandby%60+getResources().getString(R.string.smin)));
      		      
      		       childlistItem=new ArrayList<ArrayList<HashMap<String, Object>>>();
      		       childlistItem.add(bsize());
      		       childlistItem.add(estimate_data("300"));
      		       childlistItem.add(estimate_data("250"));
      		       childlistItem.add(estimate_data("400"));
      		       childlistItem.add(estimate_data("150"));
      		       childlistItem.add(estimate_data("150"));
      		       childlistItem.add(estimate_data("5"));
      		       
      		       listItemAdapter = new SimpleExpandableListAdapter(tabBatteryEstimate.this,listItem,    
      		    		    R.layout.list_item1,    
      		    	    	new String[] {"title","content"},     	 
      		    	    	new int[] { R.id.textView1, R.id.textView2 },
      		    	    	childlistItem,
      		    	    	R.layout.list_item2,
      		    	    	new String[] {"title","content"},
      		    	    	new int[] { R.id.textView1, R.id.textView2 });
      		       
      				elv.setAdapter(listItemAdapter);
	    	  
	    }
}
